#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: V1TaskDataSync.py 
@version:
@time: 2022/03/23 
"""
from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_SYNC_ATTRS_ATTRS_TASK_SETTING_DATA
from TapClientCore.T04_logic.Dag02.Dag.V1DagDataSync import V1DagDataSync


class V1TaskDataSync:

    def __init__(self):
        self.dag = V1DagDataSync()
        self.setting = TASK_CREATE_DATA_SYNC_ATTRS_ATTRS_TASK_SETTING_DATA.copy()
        self.stage = None

    def _clone(self, stage):
        self.stage = stage
        return self

    def config(self, setting):
        self.setting.update(setting)

    def readFrom(self, source):
        # readFrom 时重置 init_source
        self.setting['syncPoints'][0]['connectionId'] = source.node.get('connectionId')
        self.dag.init_source = source
        return self._clone(source)

    def customReadFrom(self, source):
        return self._clone(source)

    def writeTo(self, sink, relation=None, join=None):
        self.dag.edge(self.stage, sink, relation, join)
        # 连续写时，最后一个node变为开始节点
        self.stage = sink
        return self

    def mergeTo(self, sink, relation):
        self.dag.merge(self.stage, sink, relation)
        return self

    @property
    def task(self):
        attr = {
            'dag': self.dag.dag,
        }
        attr.update(self.setting)
        return attr
